//
//  ByRecordVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/4.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByRecordVC.h"
#import "ByRecordHeadView.h"
#import "ByTitleTFView.h"

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>

#import "ByConfirmUpdateView.h"
#import "ByHourDatePicker.h"
#import "JSONKit.h"

@interface ByRecordVC ()<CNContactPickerDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) ByTitleTFView *view1;
@property (nonatomic, strong) ByTitleTFView *view2;
@property (nonatomic, strong) ByTitleTFView *view3;
@property (nonatomic, strong) ByTitleTFView *view4;
@property (nonatomic, strong) ByTitleTFView *view5;
@property (nonatomic, strong) ByTitleTFView *view6;
@property (nonatomic, strong) ByTitleTFView *view7;
@property (nonatomic, strong) ZKBombView *dateView;
@property (nonatomic, strong) NSArray *dateArray;

@property (nonatomic, copy) NSString *selectedPhone;

@end

@implementation ByRecordVC

- (ZKBombView *)dateView{
    if (_dateView == nil) {
        _dateView = [[ZKBombView alloc] init];
        _dateView.positionMode = ZKBombPositionModeBottom;
        _dateView.popMode = ZKBombPopModeBottom;
        _dateView.usePickerTitleForCustomContentMode = YES;
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,216)];
        _dateView.contentView = contentView;
        _dateView.contentMode = ZKBombContentModeCustom;
        _dateView.title = @"选择时间";
       
        ByHourDatePicker *picker = [[ByHourDatePicker alloc] init];
        picker.frame = contentView.bounds;
        picker.dateDidPicker = ^(NSArray *dateArray){
            self.dateArray = dateArray;
            NSLog(@"year = %@,month =  %@,day =  %@",dateArray[0], dateArray[1], dateArray[2]);
        };
        [contentView addSubview:picker];
        _dateView.removeBlock = ^(){
            NSLog(@"取消");
        };
        __weak typeof(self) weakSelf = self;
        _dateView.customTapOk = ^(){
            NSLog(@"OK");
            weakSelf.view3.textTield.text = [NSString stringWithFormat:@"%@年%@月%@日", weakSelf.dateArray[0],weakSelf.dateArray[1],weakSelf.dateArray[2]];
        };
        
       
        
    }
    return _dateView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"记一笔";
    [self addLeftBackBarButtonItem];
    [self initViews];
    
    self.view5.textTield.text = @"0";
    if (self.isUpdate && self.netModel) {
        [self initData];
    }
    // Do any additional setup after loading the view.
}

//更新数据
- (void)initData{
    //工人
    if (BY_USER.isWorker.integerValue == 1){
        self.view1.textTield.text = self.netModel.overseer_name;
        self.selectedPhone = self.netModel.overseer_mobile ;
    }else{
         self.selectedPhone = self.netModel.worker_mobile ;
         self.view1.textTield.text = self.netModel.worker_name;
    }
    self.view6.textTield.text = self.netModel.project_title;
    
    NSString *normal_work_time;
    NSString *over_work_time ;
    NSString *hourly_wage ;
    
    NSMutableDictionary *dic;
    if (self.netModel.extra && self.netModel.extra.length > 0 && ![self.netModel.extra isEqualToString:@"[]"]) {
        dic = [self.netModel.extra mutableObjectFromJSONString];
        normal_work_time = dic[@"normal_worktime"];
        over_work_time = dic[@"over_worktime"];
        hourly_wage = dic[@"hourly_wage"];
    }
    
    self.view2.textTield.text = [NSString stringWithFormat:@"%.2f", hourly_wage.floatValue / 100];
    NSString* month = [self.netModel.daytime substringWithRange:NSMakeRange(4, 2)];
    NSString* day = [self.netModel.daytime substringWithRange:NSMakeRange(6, 2)];
    NSString* year = [self.netModel.daytime substringWithRange:NSMakeRange(0, 4)];
    NSString *dayStr = [NSString stringWithFormat:@"%ld",day.integerValue];
    NSString *monthStr = [NSString stringWithFormat:@"%ld", month.integerValue];
    NSLog(@"year = %@monthStr=%@,monthStr =%@", year,monthStr,dayStr);
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    [array addObject:year];
    [array addObject:monthStr];
    [array addObject:dayStr];
    self.dateArray = [array copy];
    self.view3.textTield.text = [NSString stringWithFormat:@"%@年%@月%@日", self.dateArray[0],self.dateArray[1],self.dateArray[2]];
    self.view4.textTield.text =  [NSString stringWithFormat:@"%@",normal_work_time];
    self.view5.textTield.text = [NSString stringWithFormat:@"%@",over_work_time];
    self.view7.textTield.text = self.netModel.note;
}

- (void)initViews{
    self.view.backgroundColor = rgba(248, 248, 248, 1);
    
    ByRecordHeadView *headView = [[ByRecordHeadView alloc] init];
    headView.isWorker = self.isWorker;
    [self.view addSubview:headView];
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@80);
    }];
    headView.indexAtView = ^(NSInteger index){
        NSLog(@"%ld", index);
        self.type = index;
        if (self.type == 0 || self.type == 1) {
          
            [self addOneView:self.scrollView];
            if (self.type == 0) {
                if (self.isUpdate && self.netModel) {
                    [self initData];
                }
            }
        }else{
            
            [self addAnotherView:self.scrollView];
        }
       
        
    };
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 110, SCREEN_WIDTH - 20, SCREEN_HEIGHT - STATUS_NAV - 110)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.layer.cornerRadius = 10;
    scrollView.contentSize = CGSizeMake(scrollView.size.width, 420 + 80);
    self.scrollView = scrollView;
    self.type = 0;
    [self addOneView:self.scrollView];
    
}

- (void)addOneView:(UIScrollView *)scrollView{
    for (UIView *view in scrollView.subviews) {
        [view removeFromSuperview];
    }
    
    ByTitleTFView *view1 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10, scrollView.size.width, 50)];
    [scrollView addSubview:view1];
    if (self.isWorker) {
        view1.titleLabel.text = @"工头";
    }else{
        view1.titleLabel.text = @"工人";
    }
    view1.textTield.placeholder = @"从通信录中选择工头";
    view1.textTield.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressBookAction:)];
    [view1.textTield addGestureRecognizer:tap];
    self.view1 = view1;
    
    ByTitleTFView *view2 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view1.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view2];
    self.view2 = view2;
    if (self.type == 0) {
        view2.titleLabel.text = @"工资标准";
        view2.textTield.placeholder = @"设置工资标准";
        view2.textTield.keyboardType = UIKeyboardTypeNumberPad;
        
    }else{
        view2.titleLabel.text = @"分项名称";
        view2.textTield.placeholder = @"输出项目名：如包转工，粉刷工";
        
    }
   
    ByTitleTFView *view3 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view2.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view3];
    view3.titleLabel.text = @"选择日期";
    view3.textTield.placeholder = @"选择日期";
    if (self.timeString) {
        view3.textTield.text = self.timeString;
    }
    view3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateAction:)];
    [view3.textTield addGestureRecognizer:tap3];
    self.view3 = view3;
    
    ByTitleTFView *view4 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view3.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view4];
    view4.textTield.keyboardType = UIKeyboardTypeNumberPad;
    if (self.type == 0) {
        
        view4.titleLabel.text = @"上班时长";
        view4.textTield.placeholder = @"选择上班时长";
        
    }else{
        
        view4.titleLabel.text = @"单价";
        view4.textTield.placeholder = @"项目单价";
    }
    self.view4 = view4;
    
    ByTitleTFView *view5 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view4.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view5];
    view5.textTield.keyboardType = UIKeyboardTypeNumberPad;
    if (self.type == 0) {
        
        view5.titleLabel.text = @"加班时长";
        view5.textTield.placeholder = @"选择加班时长";
        
    }else{
        
        view5.titleLabel.text = @"数量";
        view5.textTield.placeholder = @"输入数量";
        
    }
    
    self.view5 = view5;
    
    ByTitleTFView *view6 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view5.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view6];
    view6.titleLabel.text = @"所在项目";
    view6.textTield.placeholder = @"输入项目名称";
    self.view6= view6;
    
    ByTitleTFView *view7 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view6.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view7];
    view7.titleLabel.text = @"备注";
    view7.textTield.placeholder = @"输入备注信息";
    self.view7 = view7;
    
    
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [scrollView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(scrollView).offset(0);
        make.top.equalTo(view7.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(200, 40));
    }];
    saveBtn.backgroundColor = BY_THEME_COLOR;
    saveBtn.layer.cornerRadius = 20;
    if (self.isUpdate && self.netModel && self.type == 0) {
        [saveBtn setTitle:@"修改" forState:UIControlStateNormal];
    }else{
        [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    }
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addAnotherView:(UIScrollView *)scrollView{
    for (UIView *view in scrollView.subviews) {
        [view removeFromSuperview];
    }
    
    ByTitleTFView *view1 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10, scrollView.size.width, 50)];
    [scrollView addSubview:view1];
    if (self.isWorker) {
        view1.titleLabel.text = @"工头";
        view1.textTield.placeholder = @"从通信录中选择工头";
    }else{
        view1.titleLabel.text = @"工人";
        view1.textTield.placeholder = @"从通信录中选择工人";
    }
    
    view1.textTield.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressBookAction:)];
    [view1.textTield addGestureRecognizer:tap];
    self.view1 = view1;
    
    ByTitleTFView *view2 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view1.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view2];
    view2.textTield.keyboardType = UIKeyboardTypeNumberPad;
    if (self.type == 2) {
        view2.titleLabel.text = @"借支金额";
        view2.textTield.placeholder = @"输入借支金额";
        
    }else{
        view2.titleLabel.text = @"结算金额";
        view2.textTield.placeholder = @"输入结算金额";
    }
    self.view2 = view2;
   
    
    ByTitleTFView *view3 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view2.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view3];
    view3.titleLabel.text = @"选择日期";
    view3.textTield.placeholder = @"选择日期";
    view3.userInteractionEnabled = YES;
    if (self.timeString) {
        view3.textTield.text = self.timeString;
    }
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateAction:)];
    [view3.textTield addGestureRecognizer:tap3];
    self.view3 = view3;
    

    ByTitleTFView *view6 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view3.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view6];
    view6.titleLabel.text = @"所在项目";
    view6.textTield.placeholder = @"输入项目名称";
    self.view6 = view6;
    
    ByTitleTFView *view7 = [[ByTitleTFView alloc] initWithFrame:CGRectMake(0, 10 + view6.bottom, scrollView.size.width, 50)];
    [scrollView addSubview:view7];
    view7.titleLabel.text = @"备注";
    view7.textTield.placeholder = @"输入备注信息";
    self.view7 = view7;
    
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [scrollView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(scrollView).offset(0);
        make.top.equalTo(view7.mas_bottom).offset(80);
        make.size.mas_equalTo(CGSizeMake(200, 40));
    }];
    saveBtn.backgroundColor = BY_THEME_COLOR;
    saveBtn.layer.cornerRadius = 20;
    if (self.isUpdate && self.netModel && self.type == 0) {
        [saveBtn setTitle:@"修改" forState:UIControlStateNormal];
    }else{
        [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    }
    
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)saveAction:(UIButton *)btn{
   [[UIApplication sharedApplication].keyWindow endEditing:YES];
    if (self.view1.textTield.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请先选择工头"];
        return;
    }
        
    
    if (self.view2.textTield.text.length == 0) {
        if (self.type == 0) {
            [ByCoreToolAlert showOnlyText:@"请先设置工资标准"];
        }else if (self.type == 1){
            [ByCoreToolAlert showOnlyText:@"请先填写分项名称"];
        }else if (self.type == 2){
            [ByCoreToolAlert showOnlyText:@"请先填写借支金额"];
        }else if (self.type == 3){
            [ByCoreToolAlert showOnlyText:@"请先填写结算金额"];
        }
        return;
    }
    
    if (self.view3.textTield.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请先选择日期"];
        return;
    }
    
    if (self.type == 0 || self.type == 1) {
        if (self.view4.textTield.text.length == 0) {
            if (self.type == 0) {
                [ByCoreToolAlert showOnlyText:@"请先填写上班时长"];
            }else{
                [ByCoreToolAlert showOnlyText:@"请先填写单价"];
            }
            return;
        }
    }
    
    if (self.type == 0 || self.type == 1) {
        if (self.view5.textTield.text.length == 0) {
            if (self.type == 0) {
                [ByCoreToolAlert showOnlyText:@"请先填写加班时长"];
            }else{
                [ByCoreToolAlert showOnlyText:@"请先填写数量"];
            }
            return;
        }
    }
    
    if (self.view6.textTield.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请先填写所在项目"];
        return;
    }
    
    if (!self.selectedPhone || self.selectedPhone.length != 11) {
        [ByCoreToolAlert showOnlyText:@"通信录手机号有误，请重新检查"];
        return;
    }
    if (self.isUpdate && self.netModel && self.type == 0) {
        NSLog(@"保存");
        ByConfirmUpdateView *view = [[ByConfirmUpdateView alloc] init];
        view.yes = ^(){
            if (self.type == 0) {
                
                [self recordLogWorkShift];
            }else if (self.type == 1){
                [self recordLogSubWork];
            }else{
                [self recordLogMoney];
            }
        };
        view.no = ^(){
            [self dismissViewControllerAnimated:YES completion:nil];
            
        };
        [view show];
        
        
    }else{
        
        if (self.type == 0) {
            
            [self recordLogWorkShift];
        }else if (self.type == 1){
            [self recordLogSubWork];
        }else{
            [self recordLogMoney];
        }
    }

}

//记录点工项目
- (void)recordLogWorkShift{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.item_type_key = @"00S002003";
    netModel.record_uid = BY_USER.uid;
    
    if (BY_USER.isWorker.integerValue == 1){
        netModel.worker_mobile = BY_USER.mobile;
        netModel.worker_name = BY_USER.nickname;
        netModel.overseer_name = self.view1.textTield.text;
        netModel.overseer_mobile = self.selectedPhone;
    }else{
        netModel.worker_mobile = self.selectedPhone;
        netModel.worker_name = self.view1.textTield.text;
        netModel.overseer_name = BY_USER.nickname;
        netModel.overseer_mobile = BY_USER.mobile;
    }
    
    netModel.project_title = self.view6.textTield.text;
    netModel.hourly_wage = self.view2.textTield.text;
    NSInteger month = [self.dateArray[1] integerValue];
    NSInteger day = [self.dateArray[2] integerValue];
    NSString *monthStr = @"";
    NSString *dayStr = @"";
    if (month < 10) {
        monthStr = [NSString stringWithFormat:@"0%ld", month];
    }else{
        monthStr = [NSString stringWithFormat:@"%ld", month];
    }
    
    if (day < 10) {
        dayStr = [NSString stringWithFormat:@"0%ld", day];
    }else{
        dayStr = [NSString stringWithFormat:@"%ld", day];
    }
    netModel.daytime = [NSString stringWithFormat:@"%@%@%@", self.dateArray[0],monthStr,dayStr];
    netModel.normal_worktime = self.view4.textTield.text;
    netModel.over_worktime = self.view5.textTield.text;
    netModel.note = self.view7.textTield.text;
    
    [ByCoreToolAlert showLoadind:@"提交中" hasMasker:YES];
    if (self.isUpdate) {
        
        netModel.ID = self.netModel.ID;
        [ByApiRecord update:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showSuccessWithText:@"提交成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        [ByApiRecord add:netModel success:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert showSuccessWithText:@"提交成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        } failure:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
  
}


//记录包工项目
- (void)recordLogSubWork{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.item_type_key = @"00S002002";
    netModel.record_uid = BY_USER.uid;
    
    if (BY_USER.isWorker.integerValue == 1){
        netModel.worker_mobile = BY_USER.mobile;
        netModel.worker_name = BY_USER.nickname;
        netModel.overseer_name = self.view1.textTield.text;
        netModel.overseer_mobile = self.selectedPhone;
    }else{
        netModel.worker_mobile = self.selectedPhone;
        netModel.worker_name = self.view1.textTield.text;
        netModel.overseer_name = BY_USER.nickname;
        netModel.overseer_mobile = BY_USER.mobile;
        
    }
    netModel.sub_title = self.view2.textTield.text;
    netModel.daytime = [NSString stringWithFormat:@"%@%@%@", self.dateArray[0],self.dateArray[1],self.dateArray[2]];
    netModel.price = self.view4.textTield.text;
    netModel.money = self.view4.textTield.text;
    netModel.count = self.view5.textTield.text;
    netModel.project_title = self.view6.textTield.text;
    netModel.note = self.view7.textTield.text;
    
    [ByCoreToolAlert showLoadind:@"提交中" hasMasker:YES];
    [ByApiRecord add:netModel success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showSuccessWithText:@"提交成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(ByNetBaseResult *result) {
         [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    

}

//借出结算记录
- (void)recordLogMoney{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    
    netModel.record_uid = BY_USER.uid;
    
    if (BY_USER.isWorker.integerValue == 1){
        
        netModel.worker_mobile = BY_USER.mobile;
        netModel.worker_name = BY_USER.nickname;
        netModel.overseer_name = self.view1.textTield.text;
        netModel.overseer_mobile = self.selectedPhone;
        
    }else{
        
        netModel.worker_mobile = self.selectedPhone;
        netModel.worker_name = self.view1.textTield.text;
        netModel.overseer_name = BY_USER.nickname;
        netModel.overseer_mobile = BY_USER.mobile;
        
    }
    netModel.note = self.view7.textTield.text;
    netModel.project_title = self.view6.textTield.text;
    if (self.type == 2) {
        netModel.item_type_key = @"00S002004";
    }else{
        netModel.item_type_key = @"00S002005";
    }
    netModel.money = self.view2.textTield.text;
    netModel.daytime = [NSString stringWithFormat:@"%@%@%@", self.dateArray[0],self.dateArray[1],self.dateArray[2]];
    
    [ByCoreToolAlert showLoadind:@"提交中" hasMasker:YES];
    [ByApiRecord add:netModel success:^(ByNetBaseResult *result){
        
        [ByCoreToolAlert showSuccessWithText:@"提交成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(ByNetBaseResult *result){
        
        [ByCoreToolAlert showFailureWithText:result.msg];
        
    }];
}

- (void)addressBookAction:(UITapGestureRecognizer *)tap{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    CNContactPickerViewController *contactPickerVC = [[CNContactPickerViewController alloc] init];
    contactPickerVC.delegate = self;
    [self presentViewController:contactPickerVC animated:YES completion:nil];
    
}

- (void)dateAction:(UITapGestureRecognizer *)tap{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.dateView show];
}


- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)AddNewProject:(UITapGestureRecognizer *)tap{
    
}

//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
//
//   CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value;
//    NSString *text1 = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName];
//    /// 电话
//    NSString *text2 = phoneNumber.stringValue;
//}

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
    NSLog(@"联系人 = %@", contact);
    self.view1.textTield.text = contact.familyName;
    NSArray<CNLabeledValue *> *phoneArray = contact.phoneNumbers;
    
    for (CNLabeledValue *label in phoneArray){
        CNPhoneNumber *phoneNum = label.value;
        NSString *value = phoneNum.stringValue;
        NSString *pureNumbers = [[value componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
        NSLog(@"phone = %@",pureNumbers);
        self.selectedPhone = pureNumbers;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
